import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;


public final class MainView extends JFrame {
	
	// =================================================================================
	// ... Components
	// =================================================================================
	private JPanel panel = new JPanel();
	
	private ImageIcon browseFolderImg = new ImageIcon("browse_folder.jpg");
	
	private JButton inputBtn = new JButton("Input");
	private JLabel inputLbl = new JLabel("Input");
	private JTextField inputTxtFld = new JTextField(25);
	private JButton inputChooserBtn = new JButton(browseFolderImg);
	
	private JButton outputBtn = new JButton("Output");
	private JLabel outputLbl = new JLabel("Output");
	private JTextField outputTxtFld = new JTextField(25);
	private JButton outputChooserBtn = new JButton(browseFolderImg);
	
	private JButton assembleBtn = new JButton("Assemble");
	private JButton clearBtn = new JButton("Clear");
	
	private JTabbedPane tabbedPane = new JTabbedPane();
	private JPanel inputPnl = new JPanel();
	private JPanel outputPnl = new JPanel();
	private JPanel symTblPnl = new JPanel();
	
	public MainView() {
		// [ fill  the amount of space between components in the GUI
		final int fill = 10;
		// ]
		
		// =============================================================================
		// ... Initialize components
		// =============================================================================
		clearBtn.addActionListener(new ClearListener());
		tabbedPane.addTab("Input", inputPnl);
		tabbedPane.addTab("Output", outputPnl);
		tabbedPane.addTab("Symbol Table", symTblPnl);
		
		// =============================================================================
		// ... Layout the components
		// =============================================================================
		JPanel inputPnl = new JPanel();
		inputPnl.setLayout(new BoxLayout(inputPnl, BoxLayout.LINE_AXIS));
		inputPnl.add(Box.createHorizontalGlue());
		inputPnl.add(this.inputLbl);
		inputPnl.add(Box.createRigidArea(new Dimension(fill, 0)));
		inputPnl.add(this.inputTxtFld);
		inputPnl.add(Box.createRigidArea(new Dimension(fill, 0)));
		inputPnl.add(this.inputChooserBtn);
		inputPnl.add(Box.createHorizontalGlue());
		
		JPanel outputPnl = new JPanel();
		outputPnl.setLayout(new BoxLayout(outputPnl, BoxLayout.LINE_AXIS));
		outputPnl.add(Box.createHorizontalGlue());
		outputPnl.add(this.outputLbl);
		outputPnl.add(Box.createRigidArea(new Dimension(fill, 0)));
		outputPnl.add(this.outputTxtFld);
		outputPnl.add(Box.createRigidArea(new Dimension(fill, 0)));
		outputPnl.add(this.outputChooserBtn);
		outputPnl.add(Box.createHorizontalGlue());
		
		JPanel buttonPnl = new JPanel();
		buttonPnl.setLayout(new BoxLayout(buttonPnl, BoxLayout.LINE_AXIS));
		buttonPnl.add(Box.createHorizontalGlue());
		buttonPnl.add(this.assembleBtn);
		buttonPnl.add(Box.createHorizontalGlue());
		buttonPnl.add(this.clearBtn);
		buttonPnl.add(Box.createHorizontalGlue());
		
		JPanel topPnl = new JPanel();
		topPnl.setLayout(new BoxLayout(topPnl, BoxLayout.Y_AXIS));
		topPnl.add(Box.createRigidArea(new Dimension(0, fill)));
		topPnl.add(inputPnl);
		topPnl.add(Box.createRigidArea(new Dimension(0, fill)));
		topPnl.add(outputPnl);
		topPnl.add(Box.createRigidArea(new Dimension(0, fill)));
		topPnl.add(buttonPnl);
		topPnl.add(Box.createRigidArea(new Dimension(0, fill)));
		
		panel.setLayout(new BorderLayout());
		panel.add(topPnl, BorderLayout.PAGE_START);
		panel.add(tabbedPane, BorderLayout.CENTER);
		
		// =============================================================================
		// ... finalize the layout
		// =============================================================================
		this.setContentPane(this.panel);
		this.pack();

		this.setTitle("View");
		// The window closing event should probably be passed to the
		// Controller in a real program, but this is a short example.
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	private void clearTabbedPanel() {
		tabbedPane.removeAll();
		inputPnl.removeAll();
		outputPnl.removeAll();
		symTblPnl.removeAll();
	}
	
	/**
	 * Adds an ActionListener to Assemble button.
	 * 
	 * @param assembleListener
	 *            the ActionListener to be associated with the Assemble button
	 */
	public void addAssembleListner(ActionListener assembleListener) {
		this.assembleBtn.addActionListener(assembleListener);
	}
	
	/**  
	 * Clears all fields
	 */
	private class ClearListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			inputTxtFld.setText("");
			outputTxtFld.setText("");
			clearTabbedPanel();
		}
	}

}
